﻿// Copyright @ shch8.com All Rights Reserved At 2008-2-16
//开发：商创技术（www.shch8.com）望月狼
/*
·用于文本打印类。
参数：printText(打印字符,光标符号,打印速度);
事件函数 printText.showTxt:写入
控制函数 play() stop() pause()

例：
import com._public._filter.printText
var str="123456789abcdefghijklmnopqrstuvwxyz";
new printText(str,"|",50);

printText.showTxt=showTxt;//建立事件函数
function showTxt(txt) {  //返回值打印入指定数
showtxt.text=txt;
}
printText.play() //开始播放
*/
package com._public._text{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Transform;
	import flash.utils.Timer;
	
	import org.osmf.events.TimeEvent;

	public class printText{
		private var str:String;
		private var showtxt:String;
		private var pageNum_b:Number;
		private var index:Number=0;
		private var runTime:Timer;
		private var showTag:Boolean=true;
		private var tag:String;
		private var gleamTime:Number=0;
		public  var showTxt:Function;
		public  var play:Function;
		public  var stop:Function;
		public  var pause:Function;
		public function printText(txt:String,_tag:String,speed:Number) {
			str=txt;
			tag=_tag;
			runTime=new Timer(speed);
			runTime.addEventListener(TimerEvent.TIMER,timeEvent);
			runTime.stop();
			play=play_fun;
			stop=stop_fun;
			pause=pause_fun;
		}
		private function play_fun():void {
			showTxt("");
			runTime.start();
			index=0;
		}
		private function pause_fun():void {
			if (index!=0) {
				runTime.stop();
				showTxt(showtxt);
			}
		}
		private function stop_fun():void {
			if (index!=0) {
				runTime.stop();
				showTxt(str);
			}
		}
		private function timeEvent(e:TimeEvent):void {
			if (str.length>index) {
				
				if (index>str.length-3) {
					showtxt=str;//
					index+=5;
					gleamTime=8;
				} else {
					index+=Math.random()*3;
					showtxt=str.slice(0,index);
					if (showTag) {
						showTag=false;
						showTxt(showtxt+tag);
					} else {
						showTag=true;
						showTxt(showtxt);
					}
				}
			} else if (str!=showtxt && str.length!=showtxt.length) {
				index=0;
				runTime.start();
			}
			if (gleamTime>0) {
				if (gleamTime==1) {
					runTime.stop();
					showTxt(showtxt);
				} else {
					if (showTag) {
						showTag=false;
						showTxt(showtxt+tag);
					} else {
						showTag=true;
						showTxt(showtxt);
					}
				}
				gleamTime--;
			}
		}
	}
}